<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <div>
      <h1>员工管理系统</h1>
      <div v-if="!isLogin">
        <!-- href的值为空字符串时 会跳转到当前页面 javascript:void(0)废掉超链接的跳转功能 -->
        <a href="javascript:void(0)">注册</a>
        <a href="javascript:void(0)" @click="login()">登录</a>
      </div>
      <div v-else>
        <input type="text" v-model="newName" placeholder="请输入员工姓名" />
        <input type="button" value="添加员工" @click="add()" />
        <a href="javascript:void(0)" @click="logout()">退出登录</a>
      </div>
      <table border="1">
        <tr>
          <th>编号</th>
          <th>名字</th>
          <th v-if="isLogin">操作</th>
        </tr>
        <tr v-for="(name, index) in arr">
          <td>{{ index+1 }}</td>
          <td>{{ name }}</td>
          <td v-if="isLogin">
            <input type="button" value="删除" @click="del(index)" />
          </td>
        </tr>
      </table>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
    <script>
      let app = new Vue({
        el: "body>div",
        data: {
          arr: ["刘备", "关羽", "张飞"],
          isLogin: false,
          newName: "",
        },
        methods: {
          login() {
            app.isLogin = true;
          },
          logout() {
            app.isLogin = false;
          },
          del(index) {
            // 从数组中删除对象 splice(删除的位置，删除的数量)
            app.arr.splice(index, 1);
          },
          add() {
            app.arr.push(app.newName);
          },
        },
      });
    </script>
  </body>
</html>
